package com.cloudsubassemblykafka10024.offset;

import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.*;

/**
 * @Description: TODO
 * @Author zx
 * @Date 2024/12/25 17:40
 * @Version 1.0
 */
public class KafkaConsumerOffset_003 {

    public static void main(String[] args) {

        // 创建 Kafka Consumer
        Properties properties = new Properties();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.18.220:9092");
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "group4");

        // 默认配置是latest
        // properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
        // 如果 配置为 earliest，则从队列头开始消费
        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        // 配置自动提交 offset 为 false
        properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
        // 偏移量自动提交
        KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<>(properties);

        // 订阅主题
        kafkaConsumer.subscribe(Arrays.asList("messageDML003"));

        // 遍历消息队列
        while (true) {
            ConsumerRecords<String, String> consumerRecords = kafkaConsumer.poll(Duration.ofSeconds(30));
            if (!consumerRecords.isEmpty()) {
                Iterator<ConsumerRecord<String, String>> iterator = consumerRecords.iterator();

                HashMap<TopicPartition, OffsetAndMetadata> offsetMap = new HashMap<>();
                while (iterator.hasNext()) {
                    // 获取消息
                    ConsumerRecord<String, String> record = iterator.next();
                    System.out.println("消息已消费");
                    System.out.println(record.topic() + ":" + record.partition() + ":" + record.offset() + ":" + record.key() + ":" + record.value() + ":" + record.timestamp());

                    // 记录消费分区的偏移量元数据
                    offsetMap.put(new TopicPartition(record.topic(), record.partition()), new OffsetAndMetadata(record.offset() + 1));
                    // 异步提交
                    kafkaConsumer.commitAsync(offsetMap, new OffsetCommitCallback() {
                        @Override
                        public void onComplete(Map<TopicPartition, OffsetAndMetadata> offsets, Exception exception) {
                            System.out.println("偏移量提交成功:" + offsets + "exception:" + exception);
                        }
                    });
                }
            }
        }
    }

}
